<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2018/3/22
 * Time: 13:39
 */

namespace app\api\model;
use think\Model;
use think\Db;
use think\Log;
use think\Cache;
use app\common\lib\exception\ApiException;

class Reclass extends Model
{
    //学生申请重上
    public function addReClass(){
        $data = $_POST;

        if(empty($data)){
            throw new ApiException('数据不合法',401);
        }

        //reclass_stu_id int 学生id 申请重上的课程id
        if(empty($data['reclass_les_id'])){
            throw new ApiException('reclass_les_id is not found',404);
        }

        //reclass_stu_id int 学生id 申请重上的学生id
        if(empty($data['reclass_stu_id'])){
            throw new ApiException('reclass_stu_id is not found',404);
        }

        try{

            $info = $this->where([
                'reclass_les_id' => $data['reclass_les_id'],
                'reclass_stu_id' => $data['reclass_stu_id']
            ])->find();

            if($info){
                return show(config('code.error'),'该课程你已经申请重上！无需重复操作！');
            }

            $this->allowField(true)->save($data);

            return show(config('code.success'),'申请重上成功！请耐心等待工作人员联系！');
        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }
}